<?php
    $staff_name_en = addslashes($_POST['staff_name_en']);
    $staff_name_th = addslashes($_POST['staff_name_th']);
    $staff_position_en = addslashes($_POST['staff_position_en']);
    $staff_position_th = addslashes($_POST['staff_position_th']);
    
    $sql = "INSERT INTO staff (staff_name_en,staff_name_th,staff_position_en,staff_position_th,staff_position) VALUES('$staff_name_en','$staff_name_th','$staff_position_en','$staff_position_th',99)";
    $result = $db->Execute($sql);
    
    if($result){
        $msg = 'บันทึกข้อมูลเรียบร้อยแล้ว';
        $staff_id = $db->Insert_ID();
        
        if($_FILES['staff_img']['name']!='')
        {
            $handle = new Upload($_FILES['staff_img']);
            if ($handle->uploaded)
            {
                //$handle->file_new_name_body = $handle->file_src_name;
                $handle->image_resize = true;
                $handle->image_ratio_fill = true;
                $handle->image_background_color = '#ffffff';
                $handle->image_x = 173;
                $handle->image_y = 146;
                $handle->Process(APPPATH.'/images/staff');
                
                if ($handle->processed)
                {
                    $sql = 'UPDATE staff SET staff_img="'.$handle->file_dst_name.'" WHERE staff_id='.$staff_id;
                    $result =$db->Execute($sql);
                    if($result)
                    {
                        $msg = 'บันทึกข้อมูลเรียบร้อยแล้ว';
                    }
                    else
                    {
                        if(img_exists(APPPATH.'/images/staff',$handle->file_dst_name)){
                            @unlink(APPPATH.'/images/staff/'.$handle->file_dst_name);
                        }
                        $msg = 'ไม่สามารถอัพโหลดรูปได้';
                    }
                }
                else
                {
                    $msg = 'ไม่สามารถอัพโหลดรูปได้';
                }
    
            }
            else
            {
                $msg = 'ไม่สามารถอัพโหลดรูปได้';
            }
        }
    }else{
        $msg = 'ไม่สามารถบันทึกข้อมูลได้';
    }
?>

<?php
    include('header.php');
?>
<div class="grid_24" id="contents">
    <div id="query_result">
    <?php
        echo $msg , ' <img src="images/ajax-loader.gif" />';
        redirect('?m=about-us',3);
    ?>
    </div>
</div><!-- end .grid_18 -->
<div class="clear">&nbsp;</div>
<?php
    include('footer.php');
?>